{% extends "domain_base.html" %}
{% block domain_content %}
{{ set(UiContext, 'roles', roles.map(eval('(i) => i._id'))) }}
{{ set(UiContext, 'canForceJoin', model.system.get('server.allowInvite') or handler.user.hasPriv(PRIV.PRIV_MANAGE_ALL_DOMAIN)) }}
{% set _rolesSelect = roles.filter(eval("(i) => i._id !== 'guest'")).map(eval('(role) => [role._id, role._id]')) %}
<div class="section">
  <div class="section__header">
    <h1 class="section__title">{{ _('{0}: Users').format(domain.name) }}</h1>
    <div class="section__tools">
      <button class="primary rounded button" name="add_user">{{ _('Add User') }}</button>
    </div>
  </div>
  {{ noscript_note.render() }}
  <div class="section__body no-padding domain-users">
    <table class="data-table">
      <colgroup>
        <col class="col--checkbox">
        <col class="col--uid">
        <col class="col--user">
        <col class="col--role">
      </colgroup>
      <thead>
        <tr>
          <th class="col--checkbox">
            <label class="compact checkbox">
              <input type="checkbox" name="select_all" data-checkbox-toggle="user">
            </label>
          </th>
          <th class="col--uid">{{ _('User ID') }}</th>
          <th class="col--user">{{ _('Username') }}</th>
          <th class="col--role">{{ _('Role') }}</th>
        </tr>
      </thead>
      <tbody>
        {%- for role, udocs in rudocs -%}
        {%- if udocs|length > 50 -%}
          <tr>
            <td colspan="3" style="text-wrap: wrap;"><div style="max-height: 300px; overflow-y: auto">
              {%- for udoc in udocs -%}
                {% set is_disabled=(rudoc._id == handler.user._id) %}
                <input type="checkbox" data-uid="{{udoc._id}}" data-checkbox-group="user" {% if is_disabled %}disabled{% else %}data-checkbox-range{% endif %}>
                [{{udoc._id}}]{{ user.render_inline(udoc, avatar=false, badge=false) }}{% if not udoc.join %}
                  <span class="not-joined">({{ _('Not joined yet') }})</span>
                {% endif %}&nbsp;&nbsp;&nbsp;
              {%- endfor -%}
            </div></td>
            <td>{{ role }}</td>
          </tr>
        {%- else -%}
        {%- for rudoc in udocs -%}
        {% set is_disabled=(rudoc._id == handler.user._id) %}
        <tr data-uid="{{ rudoc._id }}">
          <td class="col--checkbox">
            <label class="compact checkbox">
              <input type="checkbox" data-checkbox-group="user" {% if is_disabled %}disabled{% else %}data-checkbox-range{% endif %}>
            </label>
          </td>
          <td class="col--uid">
            {{ rudoc._id }}
          </td>
          <td class="col--user">
            {{ user.render_inline(rudoc, badge=false) }}
            {% if not rudoc.join %}<span class="text-orange">({{ _('Not joined yet') }})</span>{% endif %}
          </td>
          <td class="col--role">
            {{ form.select({
                 options:_rolesSelect,
                 name:'role',
                 value:rudoc.role,
                 extra_class:'compact',
                 disabled:is_disabled
               }) }}
          </td>
        </tr>
        {%- endfor -%}
        {%- endif -%}
        {%- endfor -%}
      </tbody>
    </table>
  </div>
  <div class="section__body">
    <button class="rounded button" name="remove_selected">{{ _('Remove Selected User') }}</button>
    <button class="rounded button" name="set_roles">{{ _('Set Roles for Selected User') }}</button>
  </div>
</div>
{% endblock %}
